package cn.itsource.pethome.system.user.service.impl;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.system.atools.registerAndLogin.domain.EmailRegister;
import cn.itsource.pethome.system.logininfo.domain.Logininfo;
import cn.itsource.pethome.system.logininfo.mapper.LogininfoMapper;
import cn.itsource.pethome.system.atools.registerAndLogin.domain.PhoneRegister;
import cn.itsource.pethome.system.user.domain.User;
import cn.itsource.pethome.system.user.mapper.UserMapper;
import cn.itsource.pethome.system.user.service.IUserService;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.utils.EmailUtil;
import cn.itsource.pethome.utils.MD5Utils;
import cn.itsource.pethome.utils.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2023-01-12
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private LogininfoMapper logininfoMapper;
    @Resource
    private EmailUtil emailUtil;

    /**
     * 通过手机号注册的用户。注册成功即可使用
     * @param register
     */
    @Override
    public Long add(PhoneRegister register) {
        User user = new User();
        user.setCreatetime(new Date());             //创建时间
        user.setUsername(register.getPhone());      //用户名
        user.setPhone(register.getPhone());         //手机号
        user.setState(BaseConstants.User.STATE_NORMAL); //状态
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);     //盐值
        user.setPassword(MD5Utils.encrypByMd5(register.getPassword()+salt));//密码。通过md5加密
        userMapper.add(user);
        //同步数据到logininfo表
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setDisable(BaseConstants.Login.DISABLE_1);
        logininfo.setType(BaseConstants.Login.TYPE_USER);
        logininfoMapper.add(logininfo);
        //user和logininfo关联
        user.setLogininfoId(logininfo.getId());
        userMapper.update(user);
        return user.getId();
    }

    /**
     * 通过邮箱注册的用户。注册成功需要激活后才可以登录
     * @param register
     */
    @Override
    public void add(EmailRegister register) throws Exception{
        User user = new User();
        user.setCreatetime(new Date());             //创建时间
        user.setUsername(register.getAccount());      //用户名
        user.setEmail(register.getAccount());         //邮箱
        user.setState(BaseConstants.User.STATE_WAIT_ACTIVE); //状态待激活
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);     //盐值
        user.setPassword(MD5Utils.encrypByMd5(register.getPassword()+salt));//密码。通过md5加密
        userMapper.add(user);
        //同步数据到logininfo表
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setDisable(BaseConstants.Login.DISABLE_0);
        logininfo.setType(BaseConstants.Login.TYPE_USER);
        logininfoMapper.add(logininfo);
        //user和logininfo关联
        user.setLogininfoId(logininfo.getId());
        userMapper.update(user);
        //发送邮件给用户
        emailUtil.complexSend(user.getEmail(),user.getId(),true);
    }

    @Override
    public User findByPhone(String phone) {
        return userMapper.findByPhone(phone);
    }

    @Override
    public User findByEmail(String email) {
        return userMapper.findByEmail(email);
    }
}
